<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理者页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap-theme.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/public.css">
<script type="text/javascript" src="${contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="${contextPath}/js/jquery.fileupload.js"></script>
<script type="text/javascript" src="${contextPath}/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/common.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-default">
			<div class="panel-heading" style="height: 60px;">
				<div style="float: right; font-size: 18px;" class="text-success">
					当前共审核<span id="totalTaskNum" style="color:blue;"></span>个任务 超过<span id="thresholdValue" style="color:orange;"></span>个任务不合格时需要退回 当前不合格数为<span id="unqualifiedNum" style="color:red;">0</span>个
				</div>
				<div>待审核任务信息<span id="taskUlT" class="pull-right text-success"></span></div>
			</div>
			<div class="panel-body">
				<button type="button" id="downTask" class="btn btn-warning">下载待审核任务</button>
				<button type="button" id="qualifiedBtn" class="btn btn-success">审核合格</button>
				<button type="button" id="unqualifiedBtn" disabled="disabled" class="btn btn-danger">审核不合格</button>
				<div style="float: right;">
					<form action="${contextPath}/security/inpsUpTagAndTextGrid" method="post" name="inpsUpTagAndTextGrid" role="form" class="form-inline" enctype="multipart/form-data">
					<label for="TAG" class="control-label">选择要修改的任务</label> &nbsp;&nbsp;
					<input type="file" style="width: 75px; height: 20px; display: inline-block;" name="file" id="fileupload" autocomplete="off" multiple/>&nbsp;&nbsp;
					<span class="help-block" style="display: inline-block;" id="uploadHelp">已选择0个文件</span>&nbsp;&nbsp;
					<button id="uploadBtn" type="button" class="btn btn-primary">上传文件</button>
					</form>
				</div>
			</div>
			<table class="table table-striped table-bordered">
				<thead>
					<tr> 
						<th colspan="7">上次退回原因</th>
					</tr>
					<tr> 
						<th colspan="7" style="text-align: left;">
							<span id="sendBackReason" style="font-weight: normal;margin-left: 20px;"></span>
						</th>
					</tr>
					<tr> 
						<th colspan="6">任务列表</th>
					</tr>
					<tr>
						<th width='6%'>序号</th>
						<th width='10%'>工作者</th>
						<th width='20%'>任务名称</th>
						<th width='20%'>工作者上传时间</th>
						<th width='18%'>标注时间</th>
						<th width='15%'>最后修改用户</th>
						<th width='6%'>操作</th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
		</div>
	</div>
	<!-------------------------------- 弹出窗口 -------------------------------------------------->
	<div id="unqualifiedModal" class="modal fade">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
					<h4 class="modal-title">审核不合格</h4>
				</div>
				<div class="modal-body">
					<form class="form" role="form">
						<div class="form-group" id="reTime">
							<div class="input-group">
								<span class="input-group-addon">修改回传时间：</span>
								<input class="form-control" onkeydown="if(event.keyCode==13){return false;}" id="day" type="number" min="0" placeholder="填入数字，修改回传时间">
								<span class="input-group-addon">小时</span>
							</div>
							<div>
								<span style="color:red;" id="day_msg"></span>
							</div>
						</div>
						
						<div class="form-group" id="">
							<label class="control-label" for="unqualifiedReason">添加不合格原因</label>
							<textarea id="unqualifiedReason" class="form-control" rows="3" maxlength="200"></textarea>
							<br>
							<label class="control-label" for="unqualifiedTaskName">不合格任务名称</label>
							<textarea id="unqualifiedTaskName" class="form-control" rows="3" style="height: 150px;"></textarea>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" id="unpualifiedSubmitBtn" class="btn btn-primary">提交</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div><!-- /.modal-content -->
		</div><!-- /.modal-dialog -->
	</div><!-- /.modal -->
	<script type="text/javascript">
		var sendbackUUID = '${sendbackUUID}';
		var percent = '${percent}';
		var inspectorId = '${inspectorId}';
		var inspectorRecordId = '${inspectorRecordId}';
		var isOutsourcer = '${isOutsourcer}';
		var workGroupCode = '${workGroupCode}';
		var taskList = "";
		var unqualifiedTaskIds = [];
		$(document).ready(function(){
//		alert("sendbackUUID_"+sendbackUUID+" percent_"+percent+" inspectorId_"+inspectorId+" inspectorRecordId_"+inspectorRecordId+" isOutsourcer_"+isOutsourcer);
		
			loadTaskList();
			$("#statu").change(function(){
				$this = $(this);
				$this.val() == "0"?$("#reTime").show():$("#reTime").hide();
			});
			/*--------------------------------------下载待审核任务-------------------------------------------------------*/
			$("#downTask").click(function(){
				if(taskList.size == 0){
					alert("暂无待审核任务！");
				}else{
					$.ajax({
						type:'POST',
						data:{"list":taskList,"sendbackUUID":sendbackUUID},
						url:'${contextPath}/security/downloadSendbackTasks',
						dataType:'json',
						success:function(data){
							if(data.wrongPath != ""){
								window.open("${contextPath}"+data.wrongPath);
							}
						}
					});
				}
			});
			/*--------------------------------------审核任务-------------------------------------------------------*/
			
			$("#qualifiedBtn").click(function(){//成功
				var conWin = confirm("确定该任务审核合格吗？");
				if(conWin){
					postQualifyFn();
				}
			});
			
			$("#unqualifiedBtn").click(function(){
				var checkedNum = $(".unqualifiedCheckbox:checked").length;//已经选择的不合格任务数 
				if(checkedNum==0){  //不选择时则点击不合格按钮提示选择 什么都不执行
					alert("请选择不合格的任务~!");
				}else{
					if(isOutsourcer=="false"){
						$("#day").attr("disabled","disabled");
						$("#day").val(1);
						$("#day_msg").html("非外包商初审不用填写时间!");
					}
					$("#unqualifiedModal").modal('show');				
				}
			});
			
			$("#unpualifiedSubmitBtn").click(function(){
				var day = $("#day").val();
				if(day == "")day = 0;
				if(day == 0){
					$("#day").parent(".input-group").addClass("has-error").focus();
					return;
				}else{
					$(".has-error").removeClass("has-error");
					var resaon = $("#unqualifiedReason").val();
					if(resaon == ""){
						var comW = confirm("确定不填写不合格原因吗？");
						if(comW){
							resaon = "";
						}else{
							return;
						}
					}
					sendBackForInspectByDay(day,resaon);
					$("#unqualifiedModal").modal('hide');
				}
			});
			postQualifyFn = function(){ //审核通过
				$.ajax({
					type:'POST',
					data:{"sendbackUUID":sendbackUUID,"inspectorId":inspectorId,"workGroupCode":workGroupCode},
					url:'${contextPath}/security/auditingForModifyCompleted',
					dataType:'json',
					success:function(data){
						if($(".alert").length>0)$(".alert").remove();
						var $alertMsg = $("<div class='alert alert-dismissable'>"+data.message+"</div>");
						$alertMsg.append("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>");
						if(data.replay == "1"){
							$alertMsg.addClass("alert-success");
						}else{
							$alertMsg.addClass("alert-danger");
						}
						$(".panel-body").prepend($alertMsg);
						unqualifiedTaskIds = [];
						loadTaskList(); 
					}
				});
			};
			/*-----------------------------  不合格退回  ---------------------------------------*/
			sendBackForInspectByDay = function(day,note){
				$.ajax({
					type:'POST',
					data:{"day":day,"note":note,"inspectedTaskIds":taskList,"unqualifiedTaskIds":unqualifiedTaskIds,"inspectorId":inspectorId,"sendbackUUID":sendbackUUID,"workGroupCode":workGroupCode},
					url:'${contextPath}/security/sendBackForModifyCompleted',
					dataType:'json',
					traditional:true,
					success:function(data){
						if($(".alert").length>0)$(".alert").remove();
						var $alertMsg = $("<div class='alert alert-dismissable'>"+data.message+"</div>");
						$alertMsg.append("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>");
						if(data.replay == "1"){
							$alertMsg.addClass("alert-success");
						}else{
							$alertMsg.addClass("alert-danger");
						}
						$(".panel-body").prepend($alertMsg);
						unqualifiedTaskIds = [];
						loadTaskList(); 
					}
				});
			};
			
		});
		/*--------------------------------------加载列表-------------------------------------------------------*/
		loadTaskList = function(){
			$.ajax({
				type:'POST',
				data:{"sendbackUUID":sendbackUUID,"inspectorId":inspectorId,"inspectorRecordId":inspectorRecordId,"percent":percent},
				url:'${contextPath}/security/modifyCompletedInspectorList',
				dataType:'json',
				success:function(data){
					$("tbody").empty();
					if(data.list == ""){
						$("#sendBackReason").html("");
						$("#downTask").attr("disabled","disabled");
						$("#qualifiedBtn").attr("disabled","disabled");
						$("#unqualifiedBtn").attr("disabled","disabled");
						$("tbody").empty();
						$("tbody").append("<tr class='text-danger'><td colspan='7'>无内容</td></tr>");
					}else{
						$("#sendBackReason").html(data.sendbackReason);
						var totalTaskNum = 0;
						$.each(data.list,function(i,item){
							if(i == 0){
								taskList = item.taskId;
							}else{
								taskList +="_"+item.taskId;
							}
							
							$("tbody").append(
								"<tr id='tr_"+item.taskId+"'>"+
									"<td>"+(i+1)+"</td>"+
									"<td>"+item.workerRealName+"</td>"+
									"<td id='taskId_"+item.taskId+"'>"+item.taskName+"</td>"+
									"<td>"+item.taskUploadTime+"</td>"+
									"<td>"+item.taskMarkTime+"</td>"+
									"<td>"+item.taskStatu+"</td>"+
									"<td><input class='unqualifiedCheckbox' type='checkbox' value='"+item.workerId+"'/><input class='unqualifiedTaskId' type='hidden' value='"+item.taskId+"'/></td>"+
								"</tr>"
							);
							totalTaskNum++;
						});
						$("#totalTaskNum").html(totalTaskNum);
						var thresholdValue = Math.round(totalTaskNum*0.04);  //临界值 
						$("#thresholdValue").html(thresholdValue);
						var flag = false;
						$(".unqualifiedCheckbox").click(function(){
							var checkedNum = $(".unqualifiedCheckbox:checked").length;
							$("#unqualifiedNum").html(checkedNum);
							if(checkedNum>thresholdValue){
								if(!flag){
									flag = confirm("正确率低于96%, 此次审核任务可以退回至初审! \r是否不再提示本信息?");
								}
								$("#unqualifiedBtn").removeAttr("disabled");
							}else{
								$("#unqualifiedBtn").attr("disabled","disabled");
								flag = false;
							}
							var taskid = $(this).next().val();
							var taskName = $("#taskId_"+taskid).html();
							if($(this).get(0).checked){
								var text = $("#unqualifiedTaskName").val();
								text = text + taskName;
								text = text + "  ";
								$("#unqualifiedTaskName").val(text);
								unqualifiedTaskIds.push(taskid);
							}else{
								var text = $("#unqualifiedTaskName").val();
								taskName = taskName+"  ";
								text = text.replace(taskName,'');
								$("#unqualifiedTaskName").val(text);
								var index = unqualifiedTaskIds.indexOf(taskid);
								unqualifiedTaskIds.splice(index,1);
							}			
						});
					}
				}
			});
		};
		/*******************************多文件上传**************************************************/
			var modifyTaskIds=[];
			$("#fileupload").click(function(){
				modifyTaskIds=[];
				$(".unqualifiedTaskId").each(function(i){
					modifyTaskIds.push($(this).val());
				});
				
			});
			$("#fileupload").fileupload({
				singleFileUploads : false,
				formAcceptCharset : 'gb2312',
				typ : "POST",
				dataType : "json",
				url : "${contextPath}/security/inpsUpTagAndTextGrid",
				add : function(e, data) {
					var $this = $(this);
					data.autoUpload = false;
					var fileNum = data.files.length;
					//-------------验证开始
					var reg = /(\.|\/)(textgrid)$/i;
					for(var i = 0; i<fileNum;i++){
						if(!(reg.test(data.files[i].name))){//文件格式验证
							alert("选择文件含有格式不正确文件,请重新选择!");
							return false;
						}
					}
					//-------------验证结束
					$("#uploadHelp").text("已选择" + fileNum+ "个文件");
					$("#uploadBtn").click(function() {
						if (data.files.length == 0) {
							$("#uploadHelp").text("请选择上传的文件");
							return false;
						};
						data.submit();
						$(this).attr("disabled","disabled").text("上传中。。");
	
					});
				},
				done : function(e, result) {
					if(result.result.msg=="1"){
						alert("文件修改成功!修改了"+result.result.returnList.length+"个文件! 修改过的文件将被标注红色背景!");
						$("#uploadBtn").removeAttr("disabled").text("上传文件");
						$("#uploadBtn").unbind("click");
						$("#uploadHelp").text("已选择0个文件");
					}else if(result.result.msg=="2"){
						alert("文件修改出错,请联系系统维护人员!");
						$("#uploadBtn").text("上传文件");
					}else if(result.result.msg=="3"){
						alert("没有文件被更改,如有问题请联系系统维护人员!");
						$("#uploadBtn").removeAttr("disabled").text("上传文件");
						$("#uploadBtn").unbind("click");
						$("#uploadHelp").text("已选择0个文件");
					}
					
					for(var i = 0; i<result.result.returnList.length;i++){
						$("#tr_"+result.result.returnList[i]).find("td").each(function(){
							$(this).css("background-color","red");
						});
					}
					
				}
			});
			 //文件上传前触发事件
		    $("#fileupload").bind("fileuploadsubmit", function (e, data) {
		        data.formData = { "taskIds": modifyTaskIds };  
		    });
	</script>
</body>
</html>
